package View;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

/**
 * The abstract form
 * @see     SwingFactory
 * @see     CustomerForm
 * @see     StaffForm
 * @see     AdminForm
 * @author  TNT
 */
public class AbstractForm extends JPanel {

    protected GridBagConstraints gbc;
    private JLabel bigTitleLbl;
    private JButton okBtn, ccBtn, rsBtn;
    private int gap;

    /**
     * Construct a new abstract form
     * @param   gap the space unit between components
     * @see     SwingFactory
     */
    public AbstractForm(int gap) {
        this.gap = gap;
        setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
//        addTitleLbl();
//        addSeparator();
        gbc.anchor = GridBagConstraints.NORTHWEST;
    }

    /**
     * Set GridBag constraints for next component
     * @param   gridx horizontal position
     * @param   gridy vertical position
     * @param   gridwidth how much columns it occupies
     * @param   top top gap
     * @param   left left gap
     * @param   btm bottom gap
     * @param   right right gap
     */
    protected void setGridBagConstraints(int gridx, int gridy, int gridwidth,
            int top, int left, int btm, int right) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.insets = new Insets(top * gap, left * gap, btm * gap, right * gap);
    }

// <editor-fold defaultstate="collapsed" desc="Create Fields">
    /**
     * Add a field include a label and a component to the form
     * @param label
     * @param jComp
     * @author  TNT
     */
    public final void addField(String label, JComponent jComp) {
        setGridBagConstraints(0, gbc.gridy + 1, 1, 2, 2, 2, 2);
        add(new JLabel(label), gbc);
        setGridBagConstraints(1, gbc.gridy, 1, 1, 2, 2, 2);
        add(jComp, gbc);
    }

    /**
     * Add a comment to the form
     * @param str
     * @author  TNT
     */
    public final void addComment(String str) {
        setGridBagConstraints(1, gbc.gridy + 1, 1, -1, 2, 1, 2);
        JLabel jLabel = new JLabel(str);
        jLabel.setFont(new Font(Font.DIALOG, Font.ITALIC, 10));
        jLabel.setForeground(Color.GRAY);
        add(jLabel, gbc);
    }

    /**
     * Add a separator to the form
     * @author  TNT
     */
    public final void addSeparator() {
        setGridBagConstraints(0, gbc.gridy + 1, 3, 2, 2, 2, 2);
        gbc.fill = GridBagConstraints.BOTH;
        add(new JSeparator(), gbc);
    }

    /**
     * Add the buttons to the form
     * @author  TNT
     */
    public final void addButtonPanel() {
        JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
        okBtn = new JButton("OK");
        ccBtn = new JButton("Hủy");
        rsBtn = new JButton("Reset");

        setGridBagConstraints(0, gbc.gridy + 1, 3, 1, 1, 1, 1);
        add(jPanel, gbc);

        jPanel.add(okBtn);
        jPanel.add(ccBtn);
        jPanel.add(rsBtn);
    }
// </editor-fold>

    /**
     * Set text for the title
     * @param   str text string for the title
     */
    public void setTitleLbl(String str) {
        bigTitleLbl.setText(str);
    }

    /**
     * Set whether the form is editable by user
     * @param   editable {@code true} if this form editable;
     * {@code false} otherwise
     * @see     StaffForm#setEditable
     * @see     AdminForm#setEditable
     */
    public void setEditable(boolean editable) {
        okBtn.setVisible(editable);
        ccBtn.setVisible(editable);
        rsBtn.setVisible(editable);
    }

// <editor-fold defaultstate="collapsed" desc="Getters">
    public JButton getOkBtn() {
        return okBtn;
    }

    public JButton getCcBtn() {
        return ccBtn;
    }

    public JButton getRsBtn() {
        return rsBtn;
    }
// </editor-fold>
}
